# -*- coding: UTF-8 -*-

from app.common.Singleton import Singleton
from app.common.globle_var import GlobleVar

class MongoConn():
    _metaclass__ = Singleton
    def __init__(self):
        self.conn = GlobleVar.getConn()

    def insertOne(self, data, collName):
        coll = self.coll(collName)
        coll.insert_one(data)

    def insertMany(self, datas, collName):
        coll = self.coll(collName)
        coll.insert_many(datas)

    def queryAll(self, collName):
        coll = self.coll(collName)
        cursor = coll.find()
        return cursor

    def deleteAll(self, collName):
        coll = self.coll(collName)
        result = coll.delete_many({})
        print ("delete ", result.deleted_count)

    def delete(self, collName, condition):
        coll = self.coll(collName)
        return coll.remove(condition)


    def query(self, condition, collName):
        coll = self.coll(collName)
        cursor = coll.find(condition)
        return cursor

    def coll(self, collName):
        db = self.conn
        return db[collName]



